package com.fand.platform.authority.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dao.ResourceDao;
import com.fand.platform.authority.dto.ResourceDto;
import com.fand.platform.authority.dto.ResourceVaRuTrMoDto;

/**
 * 资源服务
 *
 * @author yaoHT
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ResourceDefaultService implements ResourceService {
	private ResourceDao resourceDao;
	
	@Override
	@CacheEvict(value = ResourceService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	@Transactional
	public ResourceDto saveResourceByIdOrCodeForResource(ResourceDto saveResource) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveResource.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		ResourceDto oldResource = this.resourceDao.selectResourceBy1ForMappedObject(ResourceDto.class,
				saveResource, null, whereEqProperties);

		if (oldResource == null) {
			this.resourceDao.insertResourceBy1(saveResource);

			return saveResource;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveResource, oldResource, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.resourceDao.updateResourceBy1(saveResource, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveResource, oldResource, updateProperties);
		}

		return oldResource;
	}

	@Override
	@Transactional
	public ResourceDto saveResourceBy2ForResource(ResourceDto saveResource) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveResource.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		ResourceDto oldResource = this.resourceDao.selectResourceBy1ForMappedObject(ResourceDto.class,
				saveResource, null, whereEqProperties);

		if (oldResource == null) {
			this.resourceDao.insertResourceBy1(saveResource, new String[] {});

			return saveResource;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveResource, oldResource, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.resourceDao.updateResourceBy1(saveResource, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveResource, oldResource, updateProperties);
		}

		return oldResource;
	}

	@Override
	@Transactional
	public int deleteResourceBy1(ResourceDto deleteResource) {
		return this.resourceDao.deleteResourceBy1(deleteResource, new String[] {});
	}

	@Override
	@Transactional
	public int deleteResourceByIds(ResourceDto deleteResource) {
		return this.resourceDao.deleteResourceByIds(deleteResource);
	}

	@Override
	@Transactional
	public int setResourceBy1(ResourceDto setResource) {
		return this.resourceDao.updateResourceBy1(setResource, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setResourceBy2(ResourceDto setResource) {
		return this.resourceDao.updateResourceBy2(setResource);
	}

	@Override
	public Integer readResourceByCodeNqIdForInteger(ResourceDto readResource) {
		return this.resourceDao.selectResourceByCodeNqIdForInteger(readResource);
	}

	@Override
	public Integer readResourceBy2ForInteger(ResourceDto readResource) {
		return this.resourceDao.selectResourceBy2ForObject(Integer.class, readResource, null);
	}

	@Override
	public List<Integer> readListResourceBy1(ResourceDto readResource) {
		return this.resourceDao.selectResourceBy1ForObjectList(Integer.class, readResource, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListResourceBy2(ResourceDto readResource) {
		return this.resourceDao.selectResourceBy2ForObjectList(Integer.class, readResource, null);
	}

	@Override
	public ResourceDto getResourceBy1(ResourceDto getResource) {
		return this.resourceDao.selectResourceBy1ForMappedObject(ResourceDto.class, getResource, null, "id");
	}

	@Override
	public ResourceDto getResourceById(ResourceDto getResource) {
		return this.resourceDao.selectResourceBy1ForMappedObject(ResourceDto.class, getResource, null, "id");
	}

	@Override
	public List<ResourceDto> findResourceBy1(ResourceDto findResource) {
		return this.resourceDao.selectResourceBy1ForMappedObjectList(ResourceDto.class, findResource,
				new String[] {}, new String[] {});
	}

	@Override
	public List<ResourceDto> findResourceBy2(ResourceDto findResource) {
		return this.resourceDao.selectResourceBy2ForMappedObjectList(ResourceDto.class, findResource);
	}

	@Override
	public PaginationDto<ResourceDto> paginationResourceBy1ForResourceList(ResourceDto paginationResource) {
		return this.resourceDao.paginationResourceBy1ForMappedObjectList(ResourceDto.class,
				paginationResource);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationResourceBy1ForMapList(ResourceDto paginationResource) {
		return this.resourceDao.paginationResourceBy1ForMapList(paginationResource);
	}

	@Override
	@Transactional
	public ResourceVaRuTrMoDto saveResourceVaRuTrMoByIdOrCodeForResourceVaRuTrMo(ResourceVaRuTrMoDto saveResourceVaRuTrMo) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveResourceVaRuTrMo.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		ResourceVaRuTrMoDto oldResourceVaRuTrMo = this.resourceDao.selectResourceVaRuTrMoBy1ForMappedObject(ResourceVaRuTrMoDto.class,
				saveResourceVaRuTrMo, null, whereEqProperties);

		if (oldResourceVaRuTrMo == null) {
			this.resourceDao.insertResourceVaRuTrMoBy1(saveResourceVaRuTrMo);

			return saveResourceVaRuTrMo;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveResourceVaRuTrMo, oldResourceVaRuTrMo, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.resourceDao.updateResourceVaRuTrMoBy1(saveResourceVaRuTrMo, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveResourceVaRuTrMo, oldResourceVaRuTrMo, updateProperties);
		}

		return oldResourceVaRuTrMo;
	}

	@Override
	@Transactional
	public ResourceVaRuTrMoDto saveResourceVaRuTrMoBy2ForResourceVaRuTrMo(ResourceVaRuTrMoDto saveResourceVaRuTrMo) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveResourceVaRuTrMo.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		ResourceVaRuTrMoDto oldResourceVaRuTrMo = this.resourceDao.selectResourceVaRuTrMoBy1ForMappedObject(ResourceVaRuTrMoDto.class,
				saveResourceVaRuTrMo, null, whereEqProperties);

		if (oldResourceVaRuTrMo == null) {
			this.resourceDao.insertResourceVaRuTrMoBy1(saveResourceVaRuTrMo, new String[] {});

			return saveResourceVaRuTrMo;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveResourceVaRuTrMo, oldResourceVaRuTrMo, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.resourceDao.updateResourceVaRuTrMoBy1(saveResourceVaRuTrMo, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveResourceVaRuTrMo, oldResourceVaRuTrMo, updateProperties);
		}

		return oldResourceVaRuTrMo;
	}

	@Override
	@Transactional
	public int deleteResourceVaRuTrMoBy1(ResourceVaRuTrMoDto deleteResourceVaRuTrMo) {
		return this.resourceDao.deleteResourceVaRuTrMoBy1(deleteResourceVaRuTrMo, new String[] {});
	}

	@Override
	@Transactional
	public int deleteResourceVaRuTrMoByIds(ResourceVaRuTrMoDto deleteResourceVaRuTrMo) {
		return this.resourceDao.deleteResourceVaRuTrMoByIds(deleteResourceVaRuTrMo);
	}

	@Override
	@Transactional
	public int setResourceVaRuTrMoBy1(ResourceVaRuTrMoDto setResourceVaRuTrMo) {
		return this.resourceDao.updateResourceVaRuTrMoBy1(setResourceVaRuTrMo, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setResourceVaRuTrMoBy2(ResourceVaRuTrMoDto setResourceVaRuTrMo) {
		return this.resourceDao.updateResourceVaRuTrMoBy2(setResourceVaRuTrMo);
	}

	@Override
	public Integer readResourceVaRuTrMoByCodeNqIdForInteger(ResourceVaRuTrMoDto readResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoByCodeNqIdForInteger(readResourceVaRuTrMo);
	}

	@Override
	public Integer readResourceVaRuTrMoBy2ForInteger(ResourceVaRuTrMoDto readResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy2ForObject(Integer.class, readResourceVaRuTrMo, null);
	}

	@Override
	public List<Integer> readListResourceVaRuTrMoBy1(ResourceVaRuTrMoDto readResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy1ForObjectList(Integer.class, readResourceVaRuTrMo, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListResourceVaRuTrMoBy2(ResourceVaRuTrMoDto readResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy2ForObjectList(Integer.class, readResourceVaRuTrMo, null);
	}

	@Override
	public ResourceVaRuTrMoDto getResourceVaRuTrMoBy1(ResourceVaRuTrMoDto getResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy1ForMappedObject(ResourceVaRuTrMoDto.class, getResourceVaRuTrMo, null, "id");
	}

	@Override
	public ResourceVaRuTrMoDto getResourceVaRuTrMoById(ResourceVaRuTrMoDto getResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy1ForMappedObject(ResourceVaRuTrMoDto.class, getResourceVaRuTrMo, null, "id");
	}

	@Override
	public List<ResourceVaRuTrMoDto> findResourceVaRuTrMoBy1(ResourceVaRuTrMoDto findResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy1ForMappedObjectList(ResourceVaRuTrMoDto.class, findResourceVaRuTrMo,
				new String[] {}, new String[] {});
	}

	@Override
	public List<ResourceVaRuTrMoDto> findResourceVaRuTrMoBy2(ResourceVaRuTrMoDto findResourceVaRuTrMo) {
		return this.resourceDao.selectResourceVaRuTrMoBy2ForMappedObjectList(ResourceVaRuTrMoDto.class, findResourceVaRuTrMo);
	}

	@Override
	public PaginationDto<ResourceVaRuTrMoDto> paginationResourceVaRuTrMoBy1ForResourceVaRuTrMoList(ResourceVaRuTrMoDto paginationResourceVaRuTrMo) {
		return this.resourceDao.paginationResourceVaRuTrMoBy1ForMappedObjectList(ResourceVaRuTrMoDto.class,
				paginationResourceVaRuTrMo);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationResourceVaRuTrMoBy1ForMapList(ResourceVaRuTrMoDto paginationResourceVaRuTrMo) {
		return this.resourceDao.paginationResourceVaRuTrMoBy1ForMapList(paginationResourceVaRuTrMo);
	}
}